/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestion;

import impresos.DatosToImprimir;
import impresos.ImpEstadisticas;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import otros.Fecha;
import otros.Util;
import tablasSql.Compras;
import tablasSql.ConsultaBD;
import tablasSql.FacturasAlbaranes;

/**
 *
 * @author FERRETERIA
 */
public class FEstadisticas extends javax.swing.JInternalFrame {

    private HashMap componentMap;
    /**
     * Creates new form FEstadisticas2
     */
    public FEstadisticas() {
        initComponents();
        createComponentMap();
        
        
        txtAño.setText(""+ new Fecha().getAño());
        //txtAño.setText("2015");
        AñadirGrafico(ComprobarAño());    
    }
    
    private void createComponentMap() {
        componentMap = new HashMap<String,Component>();
        Component[] components = jPanel1.getComponents();
        for (int i=0; i < components.length; i++) {
                componentMap.put(components[i].getName(), components[i]);
        }
    }

    public Component getComponentByName(String name) {
            if (componentMap.containsKey(name)) {
                    return (Component) componentMap.get(name);
            }
            else return null;
    }
    
    private int ComprobarAño() {
        int año;
        try {
            año = Integer.parseInt(txtAño.getText());
        } catch (Exception e) {
            año = 0;
        }
        return año;
    }
    
    private void BorrarDatos() {
        JTextField txt = null;
        for (int i = 0; i < 12; i++) { // meses
            ((JTextField) getComponentByName("txt0"+i)).setText("");
            ((JTextField) getComponentByName("txt1"+i)).setText("");
        }
    }
    
    private void RellenarMeses(DefaultCategoryDataset datos){
        int iCompras = datos.getRowIndex("Compras");
        int iVentas = datos.getRowIndex("Ventas");
        float ventas = 0;
        float compras = 0;
        JTextField txt = null;
        for (int i = 0; i<12; i++) {
           txt = null; 
           int j = datos.getColumnIndex(Fecha.getMes(i));
           if (j >= 0) {
               if (iCompras != -1) {
                    if (datos.getValue(iCompras, j) != null)
                        txt = (JTextField) getComponentByName("txt"+iCompras+i);
                    if (txt != null) {
                        txt.setText(Util.getFormatPrecio(datos.getValue(iCompras, j),2));
                        //System.err.println("[" + iCompras + "," + j + "] = " + datos.getValue(iCompras, j));
                        compras += Float.parseFloat(""+datos.getValue(iCompras, j));
                    }
               }
               if (iVentas != -1) {
                    if (datos.getValue(iVentas, j) != null)
                        txt = (JTextField) getComponentByName("txt"+iVentas+i);
                    if (txt != null) {
                        txt.setText(Util.getFormatPrecio(datos.getValue(iVentas, j),2));
                        ventas += Float.parseFloat(""+datos.getValue(iVentas, j));
                    }
                    //System.err.println("[" + iCompras + "," + j + "] = " + datos.getValue(iVentas, j));
               }
           }           
        }
        txtTotalC.setText(Util.getFormatPrecio(compras,2));
        txtTotalV.setText(Util.getFormatPrecio(ventas,2));
        txtDiferencia.setText(Util.getFormatPrecio(ventas-compras,2));
    }
    
    
    private JFreeChart CrearGrafico (int año) {
        DefaultCategoryDataset dataset = ObtenerDatos(año);
        BorrarDatos();
        RellenarMeses(dataset);
        if (dataset != null) {
            // Creando el Grafico
            JFreeChart chart = ChartFactory.createBarChart3D("Compras y Ventas " + año,"","", 
            dataset, PlotOrientation.VERTICAL, true,true, false);
            //chart.setBackgroundPaint(Color.cyan);
            chart.getTitle().setPaint(Color.BLACK); 
            CategoryPlot p = chart.getCategoryPlot(); 
            p.setRangeGridlinePaint(Color.red); 
            return chart;
        }
        return null;
    }
    
    
    private void AñadirGrafico (int año) {
        pGrafico.removeAll();
        // Datos
//        DefaultCategoryDataset dataset = ObtenerDatos(año);
//        RellenarMeses(dataset);
//        if (dataset != null) {
//            // Creando el Grafico
//            JFreeChart chart = ChartFactory.createBarChart3D("Compras y Ventas " + año,"","", 
//            dataset, PlotOrientation.VERTICAL, true,true, false);
//            //chart.setBackgroundPaint(Color.cyan);
//            chart.getTitle().setPaint(Color.BLACK); 
//            CategoryPlot p = chart.getCategoryPlot(); 
//            p.setRangeGridlinePaint(Color.red); 
//            
        // Mostrar Grafico
        JFreeChart chart = CrearGrafico(año);
        if (chart != null) {
            ChartPanel chartPanel = new ChartPanel(chart);
            pGrafico.setLayout(new BorderLayout());
            pGrafico.add(chartPanel, BorderLayout.CENTER);
        }
        pGrafico.revalidate();
    }
    
    public class DatosEstad {
        public float ventas;
        public ArrayList<String> compProv;
        public ArrayList<Float> comp;
        
        public DatosEstad() {
            ventas = 0;
            compProv = new ArrayList<String>();
            comp = new ArrayList<Float>();
        }
    }
    
    private DefaultCategoryDataset ObtenerDatos(int año) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        
        String condiciones = " and year(fecha) = " + año;
        
        String tabla = FacturasAlbaranes.TFACTURA;
        String sql = "SELECT sum( if(totalmano <> 0, if(totalmano="+ FacturasAlbaranes.PRECIOCERO +",0,totalmano),"
                    + "(1-descuento/100)*total))"
                    + " as importe, month(fecha) as mes from " + tabla + " where tipopago=0"
                    + condiciones + " group by mes";
        ResultSet resVF = FacturasAlbaranes.getDatos(sql);
        sql = sql.replace(tabla, FacturasAlbaranes.getTablaSql(1));
        ResultSet resVA = FacturasAlbaranes.getDatos(sql);
        
        tabla = Compras.TABLA;
        sql = "SELECT sum(total) as importe, "
                    + "month(fechapago) as mes from " + tabla + " where (tipopago=0 or tipopago=10) "
                    + condiciones.replace("fecha", "fechapago") + " group by mes";
        ResultSet resC = FacturasAlbaranes.getDatos(sql);
        
        try {    
            // facturas
            while (resVF.next()) {
                dataset.setValue(resVF.getFloat("importe"), "Ventas", Fecha.getMes(resVF.getInt("mes")-1));
            }
            // albaranes
            while (resVA.next()) {
                dataset.incrementValue(resVA.getFloat("importe"), "Ventas", Fecha.getMes(resVA.getInt("mes")-1));
            }
            // compras facturas y albaranes a contado
            while (resC.next()) {
                dataset.setValue(resC.getFloat("importe"), "Compras", Fecha.getMes(resC.getInt("mes")-1));
            }
            
        } catch(Exception e) {
            return null;
        }
        return dataset;
    }
    
    private void Imprimir (int mes, int año, boolean pdf) {
        try {
            float ventas = 0;
            float gastos = 0;
            int tamLista = 0;
            String condiciones = "";
            if (mes != 0) {
                condiciones = " and month(fecha) = " + mes;
            }
            String tabla = FacturasAlbaranes.TFACTURA;
            String sql = "SELECT sum( if(totalmano <> 0, if(totalmano="+ FacturasAlbaranes.PRECIOCERO +",0,totalmano),"
                    + "(1-descuento/100)*total)) as importe,"
                    + " date(fecha) as dia from " + tabla 
                    + " where year(fecha) = " + año + " and tipopago = 0" 
                    + condiciones + " group by month(fecha), day(fecha)";
            ResultSet resVF = FacturasAlbaranes.getDatos(sql);
            sql = sql.replace(tabla, FacturasAlbaranes.getTablaSql(1));
            ResultSet resVA = FacturasAlbaranes.getDatos(sql);
            
            Map<String, DatosEstad> datos = new TreeMap<String, DatosEstad>();
            DatosEstad d = null;
            while (resVF.next()) {
                d = new DatosEstad();
                d.ventas = resVF.getFloat("importe");
                ventas += resVF.getFloat("importe");
                datos.put(resVF.getString("dia"), d);
                tamLista++;
            }
            while(resVA.next()) {
                float num = resVA.getFloat("Importe");
                if (datos.containsKey(resVA.getString("dia"))) {
                    d = datos.get(resVA.getString("dia"));
                    d.ventas += num;
                    ventas += resVA.getFloat("importe");
                } 
                else {
                    d = new DatosEstad();
                    tamLista++;
                }
                datos.put(resVA.getString("dia"), d) ;
            }
                        
            tabla = Compras.TABLA;
            sql = "SELECT total as importe, "
                    + "concat_ws(' ',observacion, nproveedores.nombrecomercial) as nombre, "
                    + "date(fechapago) as dia from " + tabla + ", nproveedores" 
                    + " where (compras.tipopago=0 or compras.tipopago=10) and "
                    + tabla + ".idProve = nproveedores.codigo"
                    + condiciones.replace("fecha", "fechapago");
            ResultSet resC = FacturasAlbaranes.getDatos(sql);
            while (resC.next()) {
                if (datos.containsKey(resC.getString("dia"))) 
                    d = datos.get(resC.getString("dia"));
                else 
                    d = new DatosEstad();
                d.compProv.add(resC.getString("nombre"));
                d.comp.add(resC.getFloat("importe"));
                gastos += resC.getFloat("importe");
                datos.put(resC.getString("dia"), d) ;
                tamLista++;
            }
            DatosToImprimir dImp = new DatosToImprimir();
            dImp.setDatosEstad(datos);
            dImp.setFecha(Fecha.getMes(mes-1)+ " " + año);
            dImp.setNumCopias(tamLista);
            dImp.setTotales(new float [] {ventas, gastos});
            ImpEstadisticas.imprime(dImp, pdf);
            
        } catch (SQLException ex) {
            System.out.println("Error en Festadisticas.imprimir " + ex);
        }
    }
    
    private void ImprimirGrafico(int año) {
//        DatosToImprimir dImp = new DatosToImprimir();
//        dImp.setDatosEstad(datos);
//        dImp.setFecha(""+año);
//        dImp.setNumCopias(1);
//        
//        ImpEstadisticas.imprime(dImp, true);
        JFreeChart chart = CrearGrafico(año);
        if (chart != null) {
            ChartPanel grafico = new ChartPanel(chart);   
            grafico.createChartPrintJob();
        }
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pGrafico = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        txtAño = new javax.swing.JTextField();
        txtEneroC = new javax.swing.JTextField();
        txtEneroV = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtFebreroC = new javax.swing.JTextField();
        txtFebreroV = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtMarzoC = new javax.swing.JTextField();
        txtMarzoV = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtAbrilC = new javax.swing.JTextField();
        txtAbrilV = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtMayoC = new javax.swing.JTextField();
        txtMayoV = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtJunioC = new javax.swing.JTextField();
        txtJunioV = new javax.swing.JTextField();
        txtJulioC = new javax.swing.JTextField();
        txtJulioV = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtAgosC = new javax.swing.JTextField();
        txtAgosV = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtSepC = new javax.swing.JTextField();
        txtSepV = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtOctC = new javax.swing.JTextField();
        txtOctV = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtNovC = new javax.swing.JTextField();
        txtNovV = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtDicC = new javax.swing.JTextField();
        txtDicV = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        bImprimir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cbMes = new javax.swing.JComboBox();
        jLabel18 = new javax.swing.JLabel();
        txtTotalC = new javax.swing.JTextField();
        txtTotalV = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        txtDiferencia = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();

        setClosable(true);
        setTitle("Gráfico");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/img/icono2.png"))); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        pGrafico.setPreferredSize(new java.awt.Dimension(650, 320));

        javax.swing.GroupLayout pGraficoLayout = new javax.swing.GroupLayout(pGrafico);
        pGrafico.setLayout(pGraficoLayout);
        pGraficoLayout.setHorizontalGroup(
            pGraficoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        pGraficoLayout.setVerticalGroup(
            pGraficoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        getContentPane().add(pGrafico, gridBagConstraints);

        jPanel1.setMinimumSize(new java.awt.Dimension(650, 180));
        jPanel1.setPreferredSize(new java.awt.Dimension(650, 180));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        txtAño.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        txtAño.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAño.setText("2014");
        txtAño.setMinimumSize(new java.awt.Dimension(70, 35));
        txtAño.setPreferredSize(new java.awt.Dimension(70, 35));
        txtAño.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAñoActionPerformed(evt);
            }
        });
        txtAño.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtAñoKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 10;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 0, 3);
        jPanel1.add(txtAño, gridBagConstraints);

        txtEneroC.setEditable(false);
        txtEneroC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtEneroC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtEneroC.setName("txt10"); // NOI18N
        txtEneroC.setPreferredSize(new java.awt.Dimension(75, 20));
        txtEneroC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEneroCActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        jPanel1.add(txtEneroC, gridBagConstraints);

        txtEneroV.setEditable(false);
        txtEneroV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtEneroV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtEneroV.setName("txt00"); // NOI18N
        txtEneroV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        jPanel1.add(txtEneroV, gridBagConstraints);

        jLabel2.setText("Enero");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        jPanel1.add(jLabel2, gridBagConstraints);

        jLabel3.setText("Febrero");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        jPanel1.add(jLabel3, gridBagConstraints);

        txtFebreroC.setEditable(false);
        txtFebreroC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtFebreroC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtFebreroC.setName("txt11"); // NOI18N
        txtFebreroC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        jPanel1.add(txtFebreroC, gridBagConstraints);

        txtFebreroV.setEditable(false);
        txtFebreroV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtFebreroV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtFebreroV.setName("txt01"); // NOI18N
        txtFebreroV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        jPanel1.add(txtFebreroV, gridBagConstraints);

        jLabel4.setText("Marzo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        jPanel1.add(jLabel4, gridBagConstraints);

        txtMarzoC.setEditable(false);
        txtMarzoC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMarzoC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtMarzoC.setName("txt12"); // NOI18N
        txtMarzoC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        jPanel1.add(txtMarzoC, gridBagConstraints);

        txtMarzoV.setEditable(false);
        txtMarzoV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMarzoV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtMarzoV.setName("txt02"); // NOI18N
        txtMarzoV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        jPanel1.add(txtMarzoV, gridBagConstraints);

        jLabel5.setText("Abril");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        jPanel1.add(jLabel5, gridBagConstraints);

        txtAbrilC.setEditable(false);
        txtAbrilC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAbrilC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtAbrilC.setName("txt13"); // NOI18N
        txtAbrilC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 1;
        jPanel1.add(txtAbrilC, gridBagConstraints);

        txtAbrilV.setEditable(false);
        txtAbrilV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAbrilV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtAbrilV.setName("txt03"); // NOI18N
        txtAbrilV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 2;
        jPanel1.add(txtAbrilV, gridBagConstraints);

        jLabel6.setText("Mayo");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 0;
        jPanel1.add(jLabel6, gridBagConstraints);

        txtMayoC.setEditable(false);
        txtMayoC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMayoC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtMayoC.setName("txt14"); // NOI18N
        txtMayoC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 1;
        jPanel1.add(txtMayoC, gridBagConstraints);

        txtMayoV.setEditable(false);
        txtMayoV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMayoV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtMayoV.setName("txt04"); // NOI18N
        txtMayoV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 2;
        jPanel1.add(txtMayoV, gridBagConstraints);

        jLabel7.setText("Junio");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 0;
        jPanel1.add(jLabel7, gridBagConstraints);

        txtJunioC.setEditable(false);
        txtJunioC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtJunioC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtJunioC.setName("txt15"); // NOI18N
        txtJunioC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 1;
        jPanel1.add(txtJunioC, gridBagConstraints);

        txtJunioV.setEditable(false);
        txtJunioV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtJunioV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtJunioV.setName("txt05"); // NOI18N
        txtJunioV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 2;
        jPanel1.add(txtJunioV, gridBagConstraints);

        txtJulioC.setEditable(false);
        txtJulioC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtJulioC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtJulioC.setName("txt16"); // NOI18N
        txtJulioC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        jPanel1.add(txtJulioC, gridBagConstraints);

        txtJulioV.setEditable(false);
        txtJulioV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtJulioV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtJulioV.setName("txt06"); // NOI18N
        txtJulioV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        jPanel1.add(txtJulioV, gridBagConstraints);

        jLabel8.setText("Julio");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        jPanel1.add(jLabel8, gridBagConstraints);

        jLabel9.setText("Agosto");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        jPanel1.add(jLabel9, gridBagConstraints);

        txtAgosC.setEditable(false);
        txtAgosC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAgosC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtAgosC.setName("txt17"); // NOI18N
        txtAgosC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        jPanel1.add(txtAgosC, gridBagConstraints);

        txtAgosV.setEditable(false);
        txtAgosV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAgosV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtAgosV.setName("txt07"); // NOI18N
        txtAgosV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 5;
        jPanel1.add(txtAgosV, gridBagConstraints);

        jLabel10.setText("Septiembre");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        jPanel1.add(jLabel10, gridBagConstraints);

        txtSepC.setEditable(false);
        txtSepC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtSepC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtSepC.setName("txt18"); // NOI18N
        txtSepC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 4;
        jPanel1.add(txtSepC, gridBagConstraints);

        txtSepV.setEditable(false);
        txtSepV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtSepV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtSepV.setName("txt08"); // NOI18N
        txtSepV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 5;
        jPanel1.add(txtSepV, gridBagConstraints);

        jLabel11.setText("Octubre");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        jPanel1.add(jLabel11, gridBagConstraints);

        txtOctC.setEditable(false);
        txtOctC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtOctC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtOctC.setName("txt19"); // NOI18N
        txtOctC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 4;
        jPanel1.add(txtOctC, gridBagConstraints);

        txtOctV.setEditable(false);
        txtOctV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtOctV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtOctV.setName("txt09"); // NOI18N
        txtOctV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 5;
        jPanel1.add(txtOctV, gridBagConstraints);

        jLabel12.setText("Noviembre");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        jPanel1.add(jLabel12, gridBagConstraints);

        txtNovC.setEditable(false);
        txtNovC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNovC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtNovC.setName("txt110"); // NOI18N
        txtNovC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 4;
        jPanel1.add(txtNovC, gridBagConstraints);

        txtNovV.setEditable(false);
        txtNovV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNovV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtNovV.setName("txt010"); // NOI18N
        txtNovV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 5;
        jPanel1.add(txtNovV, gridBagConstraints);

        jLabel13.setText("Diciembre");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        jPanel1.add(jLabel13, gridBagConstraints);

        txtDicC.setEditable(false);
        txtDicC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDicC.setMinimumSize(new java.awt.Dimension(75, 20));
        txtDicC.setName("txt111"); // NOI18N
        txtDicC.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 4;
        jPanel1.add(txtDicC, gridBagConstraints);

        txtDicV.setEditable(false);
        txtDicV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDicV.setMinimumSize(new java.awt.Dimension(75, 20));
        txtDicV.setName("txt011"); // NOI18N
        txtDicV.setPreferredSize(new java.awt.Dimension(75, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 5;
        jPanel1.add(txtDicV, gridBagConstraints);

        jLabel14.setText("Compras");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        jPanel1.add(jLabel14, gridBagConstraints);

        jLabel15.setText("Ventas");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        jPanel1.add(jLabel15, gridBagConstraints);

        jLabel16.setText("Compras");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        jPanel1.add(jLabel16, gridBagConstraints);

        jLabel17.setText("Ventas");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        jPanel1.add(jLabel17, gridBagConstraints);

        bImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/imprimir.png"))); // NOI18N
        bImprimir.setToolTipText("Imprimir");
        bImprimir.setMaximumSize(new java.awt.Dimension(60, 60));
        bImprimir.setMinimumSize(new java.awt.Dimension(60, 60));
        bImprimir.setName("bImprimir"); // NOI18N
        bImprimir.setPreferredSize(new java.awt.Dimension(65, 65));
        bImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bImprimirActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 0, 0);
        jPanel1.add(bImprimir, gridBagConstraints);

        jLabel1.setText("Año");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        jPanel1.add(jLabel1, gridBagConstraints);

        cbMes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ninguno", "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 0, 3);
        jPanel1.add(cbMes, gridBagConstraints);

        jLabel18.setText("Mes");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel1.add(jLabel18, gridBagConstraints);

        txtTotalC.setEditable(false);
        txtTotalC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtTotalC.setMinimumSize(new java.awt.Dimension(85, 20));
        txtTotalC.setName("txt10"); // NOI18N
        txtTotalC.setPreferredSize(new java.awt.Dimension(85, 20));
        txtTotalC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTotalCActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        jPanel1.add(txtTotalC, gridBagConstraints);

        txtTotalV.setEditable(false);
        txtTotalV.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtTotalV.setMinimumSize(new java.awt.Dimension(85, 20));
        txtTotalV.setName("txt11"); // NOI18N
        txtTotalV.setPreferredSize(new java.awt.Dimension(85, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        jPanel1.add(txtTotalV, gridBagConstraints);

        jLabel19.setText("Compras total:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 15);
        jPanel1.add(jLabel19, gridBagConstraints);

        jLabel20.setText("Ventas total:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel1.add(jLabel20, gridBagConstraints);

        txtDiferencia.setEditable(false);
        txtDiferencia.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDiferencia.setMinimumSize(new java.awt.Dimension(85, 20));
        txtDiferencia.setName("txt11"); // NOI18N
        txtDiferencia.setPreferredSize(new java.awt.Dimension(85, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        jPanel1.add(txtDiferencia, gridBagConstraints);

        jLabel21.setText("Ganancias:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        jPanel1.add(jLabel21, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        getContentPane().add(jPanel1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtAñoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAñoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtAñoActionPerformed

    private void txtAñoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAñoKeyReleased
        if (evt.getKeyCode() == 10) {
            ConsultaBD.cursorEspera(this);
            int año = ComprobarAño();
            if (año != 0) 
                AñadirGrafico(año);
            ConsultaBD.cursorNormal(this);
        }
    }//GEN-LAST:event_txtAñoKeyReleased

    private void bImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bImprimirActionPerformed
        int op = JOptionPane.showOptionDialog(this, "Imprimir en:", "¿Que quiere hacer?", 
                                 JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE, null,
                                 new Object[] { "Normal", "Pdf" }, 
                                 "Pdf"); 
        boolean pdf = (op == 0)? false: true;
        ConsultaBD.cursorEspera(this);
        if (cbMes.getSelectedIndex() == 0)
            for(int i = 1; i < 13; i++)
                Imprimir(i,Integer.parseInt(txtAño.getText()),pdf);
        else
            Imprimir(cbMes.getSelectedIndex(),Integer.parseInt(txtAño.getText()),pdf);
        ConsultaBD.cursorNormal(this);
    }//GEN-LAST:event_bImprimirActionPerformed

    private void txtEneroCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEneroCActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtEneroCActionPerformed

    private void txtTotalCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTotalCActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTotalCActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bImprimir;
    private javax.swing.JComboBox cbMes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel pGrafico;
    private javax.swing.JTextField txtAbrilC;
    private javax.swing.JTextField txtAbrilV;
    private javax.swing.JTextField txtAgosC;
    private javax.swing.JTextField txtAgosV;
    private javax.swing.JTextField txtAño;
    private javax.swing.JTextField txtDicC;
    private javax.swing.JTextField txtDicV;
    private javax.swing.JTextField txtDiferencia;
    private javax.swing.JTextField txtEneroC;
    private javax.swing.JTextField txtEneroV;
    private javax.swing.JTextField txtFebreroC;
    private javax.swing.JTextField txtFebreroV;
    private javax.swing.JTextField txtJulioC;
    private javax.swing.JTextField txtJulioV;
    private javax.swing.JTextField txtJunioC;
    private javax.swing.JTextField txtJunioV;
    private javax.swing.JTextField txtMarzoC;
    private javax.swing.JTextField txtMarzoV;
    private javax.swing.JTextField txtMayoC;
    private javax.swing.JTextField txtMayoV;
    private javax.swing.JTextField txtNovC;
    private javax.swing.JTextField txtNovV;
    private javax.swing.JTextField txtOctC;
    private javax.swing.JTextField txtOctV;
    private javax.swing.JTextField txtSepC;
    private javax.swing.JTextField txtSepV;
    private javax.swing.JTextField txtTotalC;
    private javax.swing.JTextField txtTotalV;
    // End of variables declaration//GEN-END:variables
}
